/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.log;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/** Code for automatically posting log reports over the Internet. */
public class LogPoster {

    private static final String NEW_LINE = System.getProperty("line.separator");

    public static final String LOG_POST_URL = "http://gemini.cs.uiuc.edu/cgi-bin/postlog.cgi";
    //public static final String BUG_POST_URL = "http://alloy.mit.edu/posttest.php";

    private static String boundary="7d1fbfe5050c";
    private static String twoHyphens="--";
    private static String lineEnd="\r\n";

    /**
     * Post the given string -- which can be as large as needed -- via POST HTTP request.
     * If we get an error attempting to post the bug (e.g. if an Internet connection
     * is not available, or if the server that accepts bug reports is down), we then
     * write a dump file and ask the user to mail it to the developers.
     */
    public static boolean postLog(String logReport_) {
		BufferedReader in = null;

		DataOutputStream outStream = null;
		try {
			// open the URL connection
			URL url = new URL(LOG_POST_URL);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setUseCaches(false);
			conn.setDefaultUseCaches(false);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Connection", "Keep-Alive");
			conn.setRequestProperty("Content-Type",
					"multipart/form-data;boundary=" + boundary);

			// ++++++ Send Byte Array to Servlet /

			int bytesRead, bytesAvailable, bufferSize;
			byte[] buffer;
			int maxBufferSize = 1 * 1024 * 1024;

			// create FileInputStream to read from file
			FileInputStream fileInputStream = new FileInputStream(new File(
					logReport_));

			// open output stream to server, POST data and multipart form up to
			// the file data
			outStream = new DataOutputStream(conn.getOutputStream());
			outStream.writeBytes(twoHyphens + boundary + lineEnd);
			outStream
					.writeBytes("Content-Disposition: form-data; name=\"logfile\";"
							+ " filename=\"" + logReport_ + "\"" + lineEnd);
			outStream.writeBytes(lineEnd);

			bytesAvailable = fileInputStream.available();
			bufferSize = Math.min(bytesAvailable, maxBufferSize);
			buffer = new byte[bufferSize];

			// read file data and write it into form
			bytesRead = fileInputStream.read(buffer, 0, bufferSize);
			while (bytesRead > 0) {
				outStream.write(buffer, 0, bufferSize);
				bytesAvailable = fileInputStream.available();
				bufferSize = Math.min(bytesAvailable, maxBufferSize);
				bytesRead = fileInputStream.read(buffer, 0, bufferSize);
			}

			// send multipart form data necessary after file data
			outStream.writeBytes(lineEnd);
			outStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

			// close streams
			fileInputStream.close();
			outStream.flush();
			outStream.close();

			// read the response back from the cgi script
			in = new BufferedReader(
					new InputStreamReader(conn.getInputStream()));
			StringBuffer report = new StringBuffer();

			for (String inputLine = in.readLine(); inputLine != null; inputLine = in
					.readLine()) {
				report.append(inputLine).append(NEW_LINE);
			}

			return true;
		} catch (Exception e_) {
			return false;
		} finally {
			if (outStream != null) {
				try {
					outStream.close();
				} catch (Exception ignore) {
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (Exception ignore) {
				}
			}
		}
	} // postLog

	public static void main(String[] args) {
		System.out.println(postLog(args[0]));
	}

}  // class BugPoster
